package com.scau.yangsuiyu.estore.controller.buyer.manageBasicInfo;

import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXRadioButton;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.controller.myComponent.AddressPicker;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 买家基本资料
 * @date 2019-06-08 12:52
 * @email suiyu_yang@163.com
 */
@FXMLController
public class InfoMessageBuyer extends AnchorPane implements Initializable {
    private Buyer buyer;

    @FXML
    private ImageView photoView;

    @FXML
    private ToggleGroup sexRadioGroup;

    @FXML
    private JFXRadioButton maleRadioButton;

    @FXML
    private JFXRadioButton femaleRadioButton;

    @FXML
    private ComboBox<String> liveProvinceCB;

    @FXML
    private ComboBox<String> liveCityCB;

    @FXML
    private ComboBox<String> liveDistrictCB;

    @FXML
    private TextField liveMoreAddressTF;

    @FXML
    private ComboBox<String> bornProvinceCB;

    @FXML
    private ComboBox<String> bornCityCB;

    @FXML
    private ComboBox<String> bornDistrictCB;

    @FXML
    private TextField bornMoreAddressTF;

    @FXML
    private TextField nicknameTF;

    @FXML
    private TextField truenameTF;

    @FXML
    private JFXDatePicker datePicker;

    private File currentPhoto;
    private FileChooser photoChooser;

    private AddressPicker liveAddressPicker;
    private AddressPicker bornAddressPicker;


    public InfoMessageBuyer() {
        loadFXML();
    }

    public InfoMessageBuyer(Buyer buyer) {
        this.buyer = buyer;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/buyer/manageBasicInfo/InfoMessageBuyer.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //初始化地址选择器
        liveAddressPicker = new AddressPicker(liveProvinceCB, liveCityCB, liveDistrictCB);
        bornAddressPicker = new AddressPicker(bornProvinceCB, bornCityCB, bornDistrictCB);
        liveAddressPicker.initComboBoxes();
        bornAddressPicker.initComboBoxes();


        //初始化头像
        currentPhoto = new File(buyer.getProfilePhoto());
        ImageViewUtil.loadImage(photoView, buyer.getProfilePhoto());

        //初始化真实姓名和昵称
        if (buyer.getNickname() != null)
            nicknameTF.setText(buyer.getNickname());
        if (buyer.getTruename() != null)
            truenameTF.setText(buyer.getTruename());

        //初始化性别
        if (buyer.getSex() != null) {
            if (buyer.getSex() == 0)
                sexRadioGroup.selectToggle(maleRadioButton);
            else
                sexRadioGroup.selectToggle(femaleRadioButton);
        }

        //初始化生日
        if (buyer.getBirthday() != null) {
            Date sqlDateBirthday = buyer.getBirthday();
            LocalDate birthDayLocalDate = sqlDateBirthday.toLocalDate();

            datePicker.setValue(birthDayLocalDate);
        } else {
            datePicker.setValue(LocalDate.now());
        }
    }

    @FXML
    private void openChoosePhotoStage() {
        photoChooser = new FileChooser();
        photoChooser.setTitle("请选择你的头像");
        photoChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("所有图片", "*.*"),
                new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                new FileChooser.ExtensionFilter("PNG", "*.png")
        );

        Stage stage = new Stage();
        currentPhoto = photoChooser.showOpenDialog(stage);

        ImageViewUtil.loadImage(photoView, getProfilePhotoString());
    }

    @FXML
    private void modifyBuyerDetail() {
        buyer.setNickname(getNickNameString());
        buyer.setTruename(getTrueNameString());
        buyer.setBirthday(getBirthdaySQLDate());
        buyer.setSex(getSexByte());
        buyer.setBornAddress(getBornAddressString());
        buyer.setLiveAddress(getLiveAddressString());
        buyer.setProfilePhoto(getProfilePhotoString());

        new BuyerDao().update(buyer);

        new Alert(Alert.AlertType.INFORMATION, "修改成功！").showAndWait();
    }


    private Byte getSexByte() {
        String sex = ((RadioButton) sexRadioGroup.getSelectedToggle()).getText();

        Byte sexByte;
        if (sex.equals("男"))
            sexByte = 0;
        else
            sexByte = 1;

        return sexByte;
    }

    private String getBornAddressString() {
        StringBuilder result = new StringBuilder();
        result.append(bornAddressPicker.getAddressPickerString());
        result.append(bornMoreAddressTF.getText());
        return result.toString();
    }


    private String getLiveAddressString() {
        StringBuilder result = new StringBuilder();
        result.append(liveAddressPicker.getAddressPickerString());
        result.append(liveMoreAddressTF.getText());
        return result.toString();
    }

    private java.sql.Date getBirthdaySQLDate() {
        LocalDate selectedDate = datePicker.getValue();

        Date sqlBirthdayDate = Date.valueOf(selectedDate);

        return sqlBirthdayDate;
    }

    private String getNickNameString() {
        if (nicknameTF.getText().equals("")) {
            new Alert(Alert.AlertType.ERROR, "昵称不能为空！").showAndWait();
        }
        return nicknameTF.getText();
    }

    private String getTrueNameString() {
        return truenameTF.getText();
    }

    private String getProfilePhotoString() {
        return currentPhoto.getAbsolutePath();
    }

}
